
#include <assert.h>
#include <string.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <pcrecpp.h>
#include "site.h"
#include "wcurl.h"
#include "main.h"

static string _dummy;

wcurl::wcurl(const string& url):_url(url),_used(false),_bscript(0)
{
    _curl = curl_easy_init();
}

wcurl::~wcurl()
{
    _close();
}

void wcurl::_close()
{
    if(_curl)
        curl_easy_cleanup(_curl);
    _curl = 0;

}

size_t wcurl::writeCallbackHdr(char* buf, size_t size, size_t nmemb, void* up)
{ //callback must have this declaration
    //buf is a pointer to the data that curl has for us
    //size*nmemb is the size of the buffer
    wcurl* pthis = reinterpret_cast<wcurl*>(up);
    pthis->_header.append(buf, size*nmemb);

    return size*nmemb; //tell curl how many bytes we handled
}

size_t wcurl::writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{
//    bool  os=false;
    wcurl* pthis = reinterpret_cast<wcurl*>(up);

    pthis->_content.append(buf, size * nmemb);
/*
    char k[2] = {0,0};
    char pc = 0;
    for(size_t c = 0; c < size*nmemb; c++)
    {
//        char* pb = buf+c;
        k[0] = buf[c];



        if(k[0] == '\r' || k[0] == '\n')
        {
            if(pc!='\n')
                pthis->_content.append(" ", size);
            pc = '\n';
            continue;
        }

        if((pc == ' ' || pc == '\t')  && (k[0]==' ' || k[0]=='\t'))
        {
            continue;
        }
        // loweercase all
        if(k[0] >='A' && k[0] <='Z')
        {
            k[0] |= 0x20;
        }
        pthis->_content.append(k, size);
        pc = k[0];

    }
*/
    return size*nmemb; //tell curl how many bytes we handled
}

void wcurl::reuse()
{
    curl_easy_cleanup(_curl);
    _curl = curl_easy_init();
    _used=false;
}

const string& wcurl::header()
{
    assert(false==_used); //one query per instance

    //curl_easy_setopt( _curl, CURLOPT_CUSTOMREQUEST, "HEAD" );
    curl_easy_setopt(_curl, CURLOPT_NOBODY, 1L);

    curl_easy_setopt( _curl, CURLOPT_USERAGENT, _AGENT);
    curl_easy_setopt( _curl, CURLOPT_HEADER, 0 );
    curl_easy_setopt (_curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(_curl, CURLOPT_HEADERFUNCTION, &wcurl::writeCallbackHdr);
    curl_easy_setopt(_curl, CURLOPT_WRITEHEADER, this);
    curl_easy_setopt(_curl, CURLOPT_FILETIME, 1L);

    curl_easy_setopt( _curl, CURLOPT_URL, _url.c_str() );
    CURLcode res = curl_easy_perform( _curl );
    _used = true;
    if (CURLE_OK == res)
    {

        time_t fileTime = -1;
        curl_easy_getinfo(_curl, CURLINFO_FILETIME, &fileTime);
        if (fileTime != -1) { //otherwise unknown
            char st[64];
            sprintf (st, "%s",ctime(&fileTime));
            st[strlen(st)-1]=0;
            _date = st;
        }
        char* ct = 0;
        curl_easy_getinfo(_curl, CURLINFO_CONTENT_TYPE, &ct);
        _mime = ct ? ct : "";
        return _header;
    }
    return _dummy;
}

const string& wcurl::content(bool with_hdr)
{
   //time_t fileTime = 1;

    assert(false==_used); //one query per instance
    //curl_easy_setopt( curl, CURLOPT_CUSTOMREQUEST, "GET" );
    curl_easy_setopt( _curl, CURLOPT_HEADER, with_hdr );
    curl_easy_setopt (_curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(_curl, CURLOPT_FILETIME, 1L);
    //curl_easy_setopt(_curl, CURLINFO_CONTENT_TYPE, true);
    curl_easy_setopt( _curl, CURLOPT_USERAGENT, _AGENT);
    curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, &wcurl::writeCallback);

    curl_easy_setopt(_curl, CURLOPT_WRITEDATA, this);
#ifdef CURLOPT_HTTPAUTH
 // curl_easy_setopt(ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
#endif


    curl_easy_setopt( _curl, CURLOPT_URL, _url.c_str() );
    CURLcode res = curl_easy_perform( _curl );
    _used = true;

    if (CURLE_OK == res)
    {
        _used = true;

        time_t fileTime = -1;
        curl_easy_getinfo(_curl, CURLINFO_FILETIME, &fileTime);
        if (fileTime != -1) { //otherwise unknown
            char st[64];
            sprintf (st, "%s", ctime(&fileTime));
            st[strlen(st)-1]=0;
            _date = st;
        }
        char* ct = 0;
        curl_easy_getinfo(_curl, CURLINFO_CONTENT_TYPE, &ct);
        _mime = ct ? ct : "";

        if(_mime.find("text")!=string::npos)
            std::transform(_content.begin(), _content.end(), _content.begin(),::tolower);


        return _content;
    }

    return _dummy;
}

